package com.eliaukjq.xunlianying;

/**
 * @Author : 佩萁
 * Description :
 * @Create : 2024/7/20 - 10:38
 */
public class VolatileLongExample {
    private volatile long sharedLong = 0L;

    public synchronized void increment() {
        sharedLong++;
    }

    public synchronized long getSharedLong() {
        return sharedLong;
    }

    public static void main(String[] args) throws InterruptedException {
        VolatileLongExample example = new VolatileLongExample();

        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                example.increment();
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                example.increment();
            }
        });

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println("Final sharedLong value: " + example.getSharedLong());
    }
}
